<?php

class m_personil extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_personil() {
        $sql = "SELECT COUNT(personil_id)'total' FROM personil";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    
     function get_count_all_personil_search($params) {
        $sql = "SELECT COUNT(personil_id)'total' FROM personil WHERE personil_nama LIKE ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    // check 
    function is_exist_personil($params) {
        $sql = "SELECT * FROM personil WHERE personil_nama = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $query->free_result();
            return true;
        } else {
            return false;
        }
    } 

    // get user profil
    function get_all_personil() {
        $sql = "SELECT * FROM personil ORDER BY personil_nama ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 

    function get_all_personil_pagination($params) {
        $sql = "SELECT * FROM personil ORDER BY personil_nama ASC LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_personil_pagination_search($params) {
        $sql = "SELECT * FROM personil WHERE personil_nama LIKE ? 
                ORDER BY personil_nama ASC 
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
    function get_personil_by_id($params) {
        $sql = "SELECT * FROM personil WHERE personil_id = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 

    // insert
    function insert($params) {
        // update by this date
        $sql = "INSERT INTO personil(personil_nama, personil_picture, personil_notelpon, personil_tgllahir, mdb, mdd) VALUES (?, ?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $params);
    }

    // edit
    function edit($params) {
        // update by this date
        $sql = "UPDATE personil 
                SET personil_nama = ?,  
                personil_picture = ?, 
                personil_notelpon = ?, 
                personil_tgllahir = ?,
                mdb = ?, 
                mdd = NOW()
                WHERE personil_id = ?";
        return $this->db->query($sql, $params);
    }

    // delete
    function delete($params) {
        // update by this date
        $sql = "DELETE FROM personil WHERE personil_id = ?";
        return $this->db->query($sql, $params);
    }

} 